package cn.edu.mju.property.dao.impl;

import cn.edu.mju.property.dao.UserDao;
import cn.edu.mju.property.entity.User;
import cn.edu.mju.property.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {


    /*
    * 查询所有用户方法
    * @return 所有用户list
    * */

    @Override
    public List<User> findAll() {
        User user = null;
        List<User> Userlist = new ArrayList<User>();
        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = "select * from admin_user";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                //user.setPassword(rs.getString("password"));
                user.setUname(rs.getString("name"));
                user.setAddress(rs.getString("address"));
                user.setPhone(rs.getString("phone"));
                user.setSex(rs.getString("sex"));
                Userlist.add(user);
            }
            pstmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Userlist;
    }


    /*
     * 登入方法
     * @parm LoginUser 只有用户名密码
     * @return user 包含用户全部数据
     * */

    @Override
    public User longin(User loginUser) {
        User user = null;
        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = "select * from admin_user where username = ? and password = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,loginUser.getUsername());
            pstmt.setString(2,loginUser.getPassword());
            ResultSet rs = pstmt.executeQuery();
            if(rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
            }
            pstmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    /*
    * 添加用户方法
    * */

    @Override
    public boolean add(User user) {
        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = "insert into admin_user values(null,?,?,?,?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,user.getUsername());
            pstmt.setString(2,user.getPassword());
            pstmt.setString(3,user.getPhone());
            pstmt.setString(4,user.getSex());
            pstmt.setString(5,user.getUname());
            pstmt.setString(6,user.getAddress());
            pstmt.executeUpdate();
            pstmt.close();
            conn.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public boolean delSelected(String id) {

        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = "delete from admin_user where id = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, id);
            pstmt.executeUpdate();
            pstmt.close();
            conn.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public User findUser(String id) {
        User user = null;
        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = "select * from admin_user where id = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,id);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                //user.setPassword(rs.getString("password"));
                user.setUname(rs.getString("name"));
                user.setAddress(rs.getString("address"));
                user.setPhone(rs.getString("phone"));
                user.setSex(rs.getString("sex"));
            }
            pstmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    /*修改用户*/
    @Override
    public boolean updateUser(User user) {
        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = "update admin_user set name=?,sex=?,phone=?,address=?,password=? where id = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getUname());
            pstmt.setString(2, user.getSex());
            pstmt.setString(3, user.getPhone());
            pstmt.setString(4, user.getAddress());
            pstmt.setString(5, user.getPassword());
            pstmt.setString(6, String.valueOf(user.getId()));
            pstmt.executeUpdate();
            pstmt.close();
            conn.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    /*总记录数*/
    @Override
    public int findTotalCount() {
        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = "select count(*) from admin_user";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            rs.next();
            int num =  rs.getInt("count(*)");
            pstmt.close();
            conn.close();
            return num;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

    }


    /*分页查询方法*/
    @Override
    public List<User> findByPage(int start, int rows) {
        User user = null;
        List<User> Userlist = new ArrayList<User>();
        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = "select * from admin_user limit ? , ? ";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,start);
            pstmt.setInt(2,rows);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                //user.setPassword(rs.getString("password"));
                user.setUname(rs.getString("name"));
                user.setAddress(rs.getString("address"));
                user.setPhone(rs.getString("phone"));
                user.setSex(rs.getString("sex"));
                Userlist.add(user);
            }
            pstmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Userlist;
    }

}
